package com.kura.uednews.fragment;

import java.util.List;

import com.kura.uedews.R;
import com.kura.uednews.MainActivity;
import com.kura.uednews.db.newsDB;
import com.kura.uednews.model.ToDoItem;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class fragment_todo_list extends Fragment {

	ListView todoListView;
	ToDoAdapter adapter;
	Button themMoiBtn;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Choose layout
		View view = inflater.inflate(R.layout.fragment_todo_list, container,
				false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		// get instance
		todoListView = (ListView) getActivity().findViewById(R.id.todoListView);

		// create adapter for listview
		adapter = new ToDoAdapter(getActivity());

		// set adapter
		todoListView.setAdapter(adapter);

		// get instance button
		themMoiBtn = (Button) getActivity().findViewById(R.id.themBtn);

		// set event
		themMoiBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FragmentTransaction tx = getFragmentManager()
						.beginTransaction();
				tx.replace(R.id.main, android.app.Fragment.instantiate(
						getActivity(),
						"com.kura.uednews.fragment.fragment_todo_item"));
				tx.commit();
			}
		});
	}

}

class ToDoAdapter extends BaseAdapter {

	Context context;
	newsDB db;
	List<ToDoItem> list;
	TextView todoTitle;
	CheckBox todoDone;

	public ToDoAdapter() {
		// TODO Auto-generated constructor stub
	}

	public ToDoAdapter(Context c) {
		this.context = c;
		db = new newsDB(context);
		list = db.getListToDo();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View row = null;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.custom_cate_row, parent, false);
		} else {
			row = convertView;
		}
		todoTitle = (TextView) row.findViewById(R.id.cateTitle);
		todoDone = (CheckBox) row.findViewById(R.id.cateSubs);
		todoDone.setOnCheckedChangeListener(null);
		todoTitle.setText(list.get(position).getName());
		if (list.get(position).getDone() == 1) {
			todoDone.setChecked(true);
		}else{
			todoDone.setChecked(false);
		}
		
		todoDone.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					db.setDone(list.get(position).getName());
				} else {
					db.setUndone(list.get(position).getName());
				}
			}
		});

		return row;
	}

}